package com.buaa.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.buaa.dao.CxbqDao;
import com.buaa.service.CxbqService;

@Service
public class CxbqServiceImpl implements CxbqService {

	@Autowired
	private CxbqDao cxbqDao;

	@Override
	public String query(String shuru, String id, String type) {
		String sql = "";
		String res = "";

		if (id == null)
			return res;
		if (id.equals("hljyqmc")) { // 仪器
			sql = "select distinct name_zh from  instrument  ";

			if (shuru != null && !shuru.equals("")) {
				shuru = shuru.replace("'", "");
				if (type.equals("cn"))
					sql += " where name_zh  like '" + shuru + "%'";
				else
					sql += " where name_en  like '" + shuru + "%'";
			}
		} else if (id.equals("hljssjg")) { // 机构
			sql = "select distinct name from  institution   ";

			if (shuru != null && !shuru.equals("")) {
				shuru = shuru.replace("'", "");
				if (type.equals("cn"))
					sql += " where name  like '" + shuru + "%'";
				else
					sql += " where name  like '" + shuru + "%'";
			}
		} else if (id.equals("hljcgmc")) { // 成果
			sql = "select distinct name from  result   ";

			if (shuru != null && !shuru.equals("")) {
				shuru = shuru.replace("'", "");
				if (type.equals("cn"))
					sql += " where name  like '" + shuru + "%'";
				else
					sql += " where name  like '" + shuru + "%'";
			}
		} else if (id.equals("hljrymc")) { // 人才
			sql = "select distinct name from  talent   ";

			if (shuru != null && !shuru.equals("")) {
				shuru = shuru.replace("'", "");
				if (type.equals("cn"))
					sql += " where name  like '" + shuru + "%'";
				else
					sql += " where name  like '" + shuru + "%'";
			}
		} else if (id.equals("hljjdmc")) { // 基地
			sql = "select distinct name from  base   ";

			if (shuru != null && !shuru.equals("")) {
				shuru = shuru.replace("'", "");
				if (type.equals("cn"))
					sql += " where name  like '" + shuru + "%'";
				else
					sql += " where name  like '" + shuru + "%'";
			}
		} else if (id.equals("hljxmmc")) { // 项目
			sql = "select distinct type from project ";

			if (shuru != null && !shuru.equals("")) {
				shuru = shuru.replace("'", "");
				if (type.equals("cn"))
					sql += " where type  like '" + shuru + "%'";
				else
					sql += " where type  like '" + shuru + "%'";
			}
		}
		System.out.println(sql);
		List<Object> list = cxbqDao.query(sql);
		if (list == null)
			return res;
		for (int i = 0; i < Math.min(10, list.size()); i++) {
			res += list.get(i) + ",";
		}
		return res;
	}

}
